/**
 * @file bsp_pwm.c
 * @brief 基于定时器的PWM驱动
 * @author wangh (wanghuan3037@fiberhome.com)
 * @version 1.0
 * @date 2021-03-19
 * @copyright Copyright (c) 2021  烽火通信
 */ 
#include "bsp_pwm.h"
 
//TIM3 PWM初始化
//arr   重装载值
//psc   预分频系数
// void TIM3_PWM_Init(u16 arr,u16 psc)
// {
//     GPIO_InitTypeDef     GPIO_InitStrue;
//     TIM_OCInitTypeDef     TIM_OCInitStrue;
//     TIM_TimeBaseInitTypeDef     TIM_TimeBaseInitStrue;


//     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);        //使能TIM3和相关GPIO时钟
//     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);// 使能GPIOB时钟
//     (LED在BP5引脚),使能AFIO时钟(定时器3通道2需要重映射到BP5引脚)
//     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

//     GPIO_InitStrue.GPIO_Pin=GPIO_Pin_5;     // TIM_CH2
//     GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;    // 复用推挽
//     GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;    //设置最大输出速度
//     GPIO_Init(GPIOB,&GPIO_InitStrue);                //GPIO端口初始化设置

//     GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);

//     TIM_TimeBaseInitStrue.TIM_Period=arr;    //设置自动重装载值
//     TIM_TimeBaseInitStrue.TIM_Prescaler=psc;        //预分频系数
//     TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up;    //计数器向上溢出
//     TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1;        //时钟的分频因子，起到了一点点的延时作用，一般设为TIM_CKD_DIV1
//     TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue);        //TIM3初始化设置(设置PWM的周期)

//     TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM2;        // PWM模式2:CNT>CCR时输出有效
//     TIM_OCInitStrue.TIM_OCPolarity=TIM_OCPolarity_High;// 设置极性-有效为高电平
//     TIM_OCInitStrue.TIM_OutputState=TIM_OutputState_Enable;// 输出使能
//     TIM_OC2Init(TIM3,&TIM_OCInitStrue);        //TIM3的通道2PWM 模式设置

//     TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);        //使能预装载寄存器

//     TIM_Cmd(TIM3,ENABLE);        //使能TIM3
// }


int8_t drv_pwm_init(Pwm_t *obj)
{
#ifdef STM32F10X

#elif defined GD32F10X
    rcu_periph_clock_enable(RCU_TIMER1);

    timer_deinit(TIMER1);

    timer_parameter_struct timer_initpara;
    timer_initpara.prescaler         = (SystemCoreClock / obj->pwm_frequency) - 1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;    /* 边沿对齐向上计数 */
    timer_initpara.period            = 15999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1, &timer_initpara);

    timer_oc_parameter_struct timer_ocintpara;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
#endif
}


int8_t drv_pwm_set(Pwm_t *obj, uint8_t channel, uint32_t period, uint32_t pulse)
{
#ifdef STM32F10X

#elif defined GD32F10X
    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 3999);
    timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
#endif
}



/*********** (C) COPYRIGHT 2021 FiberHome *****END OF FILE****/
